package labs.laba_03;

import java.math.BigDecimal;
import java.util.ArrayList;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;

public class FTriangle extends Figure {

	public FTriangle(float center_x, float center_y, Figure parent, FigureTypes ft){
		super(center_x, center_y, parent, ft);
	}
	private Dot[] getDots(){
		Dot dts[] = new Dot[3];
		float dif = BigDecimal.valueOf(this.width/2).setScale(3).floatValue();
		//float dif2 = BigDecimal.valueOf(((2.0f/3.0f)*dif)).setScale(3).floatValue();
		dts[0] = new Dot(this.center.getX()-dif,	this.center.getY()+dif);
		dts[1] = new Dot(this.center.getX(),		this.center.getY()-dif);
		dts[2] = new Dot(this.center.getX()+dif,	this.center.getY()+dif);
		return dts;
	}
	@Override
	public float getOuterRadius() {
		return (float)Math.sqrt(Math.pow((width/2), 2)+Math.pow((width/2), 2));
	}
	@Override
	public ArrayList<PrimitiveLine> getPrimitive() {
		ArrayList<PrimitiveLine> pl_list = new ArrayList<PrimitiveLine>(3);
		Dot dots[] = getDots();
		short i;
		for(i = 0; i<dots.length-1; i++){
			pl_list.add(new PrimitiveLine(dots[i], dots[i+1]));
		}
		pl_list.add(new PrimitiveLine(dots[i], dots[0]));
		return pl_list;
	}
	@Override
	public void draw(Canvas canvas) {
		Dot dots[] = getDots();
		Path mPath = new Path();
		mPath.setFillType(Path.FillType.EVEN_ODD);
		mPath.moveTo(dots[0].getX(), dots[0].getY());
		for(short i=1; i<dots.length; i++){
			mPath.lineTo(dots[i].getX(), dots[i].getY());			
		}
		mPath.lineTo(dots[0].getX(), dots[0].getY());
		mPath.close();
		//
		Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);            
		mPaint.setStrokeWidth(2);
		//paint.setShader(new LinearGradient(0, 0, 0, getHeight(), Color.BLACK, Color.WHITE, Shader.TileMode.MIRROR));
		mPaint.setARGB(255, 50, 100, 200);
		mPaint.setStyle(Paint.Style.FILL);
		mPaint.setAntiAlias(true);
		canvas.drawPath(mPath, mPaint);
	}
}
